package cn.hunqing.web.util;

import javax.servlet.http.HttpSession;

import cn.hunqing.web.session.SessionUserInfo;

/**
 * Check the user session
 * 
 * @author Kai Zhang
 *
 */
public class SessionUtils {
	public static boolean isLogin(HttpSession session) {
		return null != session && "true".equals(session.getAttribute(SessionConstants.LOGIN));
	}

	public static void doLogin(HttpSession session, SessionUserInfo sessionUserInfo) {
		session.setAttribute(SessionConstants.LOGIN, "true");
		session.setAttribute(SessionConstants.SESSION_USER_INFO, sessionUserInfo);
	}

//	public static void doLogin(YdxSessionLoginCallback callback) {
//		callback.beforeLogin();
//		callback.getHttpSession().setAttribute(SessionConstants.LOGIN, "true");
//		callback.getHttpSession().setAttribute(SessionConstants.USERID, callback.getUserId());
//		callback.afterLogin();
//	}

	public static Integer getUserId(HttpSession session) {
		Object obj = session.getAttribute(SessionConstants.SESSION_USER_INFO);
		if(obj != null && obj instanceof SessionUserInfo){
			SessionUserInfo sessionUserInfo = (SessionUserInfo) obj;
			return sessionUserInfo.getId();
		}
		return null;
	}
	
	public static String getUserName(HttpSession session) {
		Object obj = session.getAttribute(SessionConstants.SESSION_USER_INFO);
		if(obj != null && obj instanceof SessionUserInfo){
			SessionUserInfo sessionUserInfo = (SessionUserInfo) obj;
			return sessionUserInfo.getUserName();
		}
		return null;
	}
	
	public static SessionUserInfo getSessionUser(HttpSession session) {
		Object obj = session.getAttribute(SessionConstants.SESSION_USER_INFO);
		if(obj != null && obj instanceof SessionUserInfo){
			return (SessionUserInfo) obj;
		}
		return null;
	}

}
